package com.iflytek.esb.function;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.iflytek.esb.domain.Message;
import com.iflytek.esb.exceptions.RetryableException;
import com.iflytek.esb.util.SoapUtil;
import com.opensymphony.module.propertyset.PropertySet;
import com.opensymphony.workflow.WorkflowException;

public class WebServiceReceiver extends MessageFunction {
	
	private static final Logger LOG = Logger.getLogger(WebServiceReceiver.class);
	
	private static final String ONE_WAY = "one-way";
	private static final String REQUEST_RESPONSE = "request-response";
	
	protected void process(Message message, 
			final Map<String, String> args,
			PropertySet ps) throws WorkflowException, RetryableException {

		LOG.debug("<invoke web service receiver>");
		
		String type = args.get("type");
		
		//消息体截取soap body
		//String body = SoapUtil.getBody(message.getBody());
		//message.setBody(body);
		
		//处理返回值，如果接收器是one-way类型，要立刻返回消息
		if (StringUtils.equals(type, ONE_WAY)) {
			message.setResponsed(true);
			message.setResponseHeaders(message.getHeaders());
			message.setResponseContent(SoapUtil.EMPTY);
		}
		else if (StringUtils.equals(type, REQUEST_RESPONSE)) {
			//do nothing
		}
	}

}
